package com.service.impl;

import com.dao.ActivityDevMapper;
import com.dao.ActivityMapper;
import com.dao.ActivityParticipateMapper;
import com.entity.*;
import com.entity.Class;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.ActivityParticipateService;
import com.service.ActivityService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ActivityParticipateServiceImpl implements ActivityParticipateService {

    @Autowired
    private ActivityParticipateMapper activityParticipateMapper;
    @Autowired
    private ActivityDevMapper activityDevMapper;

    public boolean addClassa(List<Class> classs){
        for (Class classe : classs){
            if (activityParticipateMapper.judgeExistenceClass(classe)==null){
                if(activityParticipateMapper.addClassa(classs)==1){
                    return true;
                }else {
                    return false;
                }
            }else {
                continue;
            }
        }
        return true;
    }

    public Object addStudent(List<Student> students) {
        Data data = new Data();
        System.out.println(students);
        Iterator<Student> it = students.iterator();
        List<Integer> devNo = new ArrayList<Integer>();
        List<Integer> repeat = new ArrayList<Integer>();
        //遍历学生判断学生和编号是否存在
        while (it.hasNext()) {
            Student s = it.next();
            if (s==null)
                break;
            System.out.println(s.getDevID()+"111");
            Device device = activityDevMapper.sleectDeviceByID(s.getDevID());

            Student student = activityParticipateMapper.selectStudent(s);
            if (student != null) {
                repeat.add(s.getIdx());
                it.remove();
                continue;
            }
            if (device == null) {
                devNo.add(s.getIdx());
                it.remove();
                continue;
            } else if (device.isWork() == true) {
                devNo.add(s.getIdx());
                it.remove();
                continue;
            }
        }
        //是否用不符合规则的学生，有就返回。添加停止
        if (devNo.size() != 0 || repeat.size() != 0) {
            Map<Object, Object> map = new HashMap<Object, Object>();
            map.put("devNo", devNo);
            map.put("repeat", repeat);
            data.setState(false);
            data.setError(map);
            return data;
        }
        for (Student itme : students){
            activityDevMapper.updataDevice(itme.getDevID());
         }


       activityParticipateMapper.addStudent(students);
        data.setState(true);
        return data;
    }
    public List<Class> selectClasss(int proID,String school){
        List<Class> list = activityParticipateMapper.selectClasss(proID,school);
        System.out.println(list);
       return  list;
    }

    public List<Student> selectStudentS(Student student){
        return  activityParticipateMapper.selectStudentS(student);
    }


    public Object selectInputStudents(String name,int proID){
        Data data = new Data();

        data.setState(true);
        if (name.equals("")){
            return data;
        }
        data.setData(activityParticipateMapper.selectInputStudents( name, proID));
        return data;
    }


    public int changeCase(Student student){
        return activityParticipateMapper.changeCase(student);
    }

    @Transactional
    public Object addDorm(List<Dorm> dorms){
        Data data = new Data();
        data.setState(true);
        try {
//            for (Dorm dorm :dorms){
//                String error = new String( "以下寝室重复或已添加：");
//                if (activityParticipateMapper.selectDorm(dorm.getDorNumber(),dorm.getBase()) != null){
//                    error = error+dorm.getBase()+":"+dorm.getDorNumber()+",";
//                    dorms.remove(dorm);
//                    data.setState(false);
//                }
//                if (data.isState() == false){
//                    data.setError(error);
//                }
//            }
            if (activityParticipateMapper.addDorm(dorms) == 0){
                data.setError("添加失败");
                data.setState(false);
                return data;
            }
        }catch (Exception e){
            e.printStackTrace();
            data.setError("添加失败");
            data.setState(false);
            return data;
        }

        return data;
    }
    public Object selectDorms(Paging paging){
        Data data = new Data();
        try {
            PageHelper.startPage(paging.getPage(), paging.getLimit());
            Page<Dorm> page  =  (Page<Dorm>) activityParticipateMapper.selectDorms();

            data.setData(JSONObject.fromObject("{\"total\":"+page.getTotal()+",\"dorms\":"+ JSONArray.fromObject(page)+"}"));
        }catch (Exception e){
            e.printStackTrace();
            data.setError("查询失败");
            data.setState(false);
            return data;
        }
        data.setState(true);
        return data;
    }

    public Object delectDorm(List<Integer> dormIDs){
        Data data = new Data();
        try {
            for (int dormID:dormIDs){
                if (activityParticipateMapper.delectDorm(dormID) !=1 ){
                    data.setState(false);
                    data.setError("删除失败");
                    return data;
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            data.setState(false);
            data.setError("删除失败");
            return data;
        }
        data.setState(true);
        return data;
    }

    public Object intputQueryDorms(String params){
        Data  data = new Data();

        if (params == null && params.equals("")){
            data.setState(true);
            return data;
        }
        try {
            List<Dorm> dorms = activityParticipateMapper.intputSeletDorms(params);
            data.setData(dorms);
        }catch (Exception e){
            e.printStackTrace();
            data.setState(false);
            data.setError("查询失败");
        }
        data.setState(true);
        return data;
    }
}
